/*
*	Purpose: Allows Lua to control the layout / design of the GUI / events
*				through an ID system.
*
*	Type: Singleton
*
*
*
*	NOTE:	The way the rendering / selection is currently handled will probably be
*				very slow, because it goes through over 700 panels. (over 2100 in the
*				three functions combined)  I'm thinking of solving this by creating a
*				vector that indexes the numbers of created panels, which would 
*				drastically increase speed.
*
*/

#pragma once

#include "Globals.hpp"
#include "GUIPanel.h"
#include "GUIPanelButton.h"



#define GUI GUIManager::Instance()

class GUIManager
{

private:
	GUIManager(const GUIManager&);
	GUIManager& operator =(const GUIManager&);
	~GUIManager(){};


	static map<int, GUIPanel>				IDToGUIPanel;					// panelID to the GUIPanel
	static map<int, vector<GUIPanelButton>> IDToGUIPanelButton;				// panelID to that panel's buttons
	static map<int, SDL_Rect>				IDToSDLRect;					// used for button locations
	static map<int, Dimension>				IDToRelativeStartingPosition;	// used to determine how far away the button starts from the panel
	static map<int, Coords>					IDToPanelLocation;				// stores the panel location
	static vector<int>						ActivatedPanels;				// This stores the ID values that have been activated

	int m_panelID;	// What is the current panel that we are working with?
	
	


public:
	static GUIManager* Instance();

	GUIManager();

	void Init(); // Set values to all of the maps
	void Destroy();

	void ActivatePanel();
	void DeactivePanel();

	// Functions used for panel drawing
	void Render();	// Renders all active GUI elements.

	// Update the elements that need to be updated
	void Update();

	// Gets if the user's mouse is in a panel
	bool GetPanelBounds(int x, int y);

	Coords GetPanelLocation() {return IDToGUIPanel[m_panelID].GetCoords();}	// Gets the location of the panel.

	int GetActivePanelButtonID(const Vector2D& mouse);	// Does not ignore the panelID and returns the ID of the button that the mouse is over
	int GetButtonID(int mouseX, int mouseY);			// Ignores the panelID and returns if the ID that the mouse is clicking on
	vector<int> GetAllButtonID();						// Returns all of the button IDs on the current panel
	void SetButtonShading(bool cond, int referenceID);	// Should the button's rectangle be drawn; works on current panel
	void SetButtonShading(bool cond);					// Should the panel's buttons be drawn; works on current panel
	void SwapButton(bool displayNormal, int referenceID); // True -> display "normalImage" False -> display "mouseoverImage"
	void SwapButton(bool displayNormal);				// Sets all buttons to display / not display

	// Adds a button to a specified panel
	void AddButton(int referenceID, string imageID);
	void AddButton(int referenceID, string normalImage, string mouseoverImage);
	void AddButton(int referenceID, string imageID, int x, int y, int w, int h);
	void AddButton(int referenceID, string normalImage, string mouseoverImage, int x, int y, int w, int h);
	void ClearButtons();

	// How far away are the buttons from the outside of the panel
	void SetRelButDist(int x, int y);  // Set Relative Button Distance from the top right of the panel


	// Add additional panels / rectangles to the GUI panel
	void AddMiniPanel(int x, int y, int w, int h, const OGL_Color& color)
			{IDToGUIPanel[m_panelID].AddPanel(x, y, w, h, color); IDToGUIPanel[m_panelID].SetAlpha(color.a);}

	void AddText(int x, int y, const char* text, const OGL_Color& color, const char* font_name)
			{IDToGUIPanel[m_panelID].AddText(x, y, text, color, font_name);}

	void AddText(const char* text, const OGL_Color& color, const char* font_name);	// Adds automatically positioned text
	void ClearText();	// Clears all the text created

	



	// Lua Functions ---------------------
	void CreatePanel();										// Creates a panel with all necessary information automatically generated
	void SetLocation(int x, int y, int w, int h);
	void SetColor(OGL_Color color);
	void SetButtonDimensions(int w, int h);
	void SetFadeTime(float val);
	void RegisterPanel(int panelID) {m_panelID = panelID;}	// Basically, we register this ID so that every other call will affect the registered panel


	// Number of buttons in current panel
	int NumButtons();
};